<?php
defined('BASEPATH') or exit('No direct script access allowed');
use \QCloud_WeApp_SDK\Conf as Conf;

class Index extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('Store_model');
        $this->load->model('Customer_model');
    }

    /**
     * @SWG\Post(
     *     summary="获取店铺信息",
     *     path="/index/store",
     *     tags={"客户端"},
     *     operationId="1",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="店铺id",
     *         in="formData",
     *         name="shop_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function store()
    {
        $post = $this->input->post();
        if ($post['shop_id']) {
            $result = $this->Store_model->getStoreInfo($post['shop_id']);
            if ($result) {
                send_json(array('code' => 2000, 'data' => $result, 'msg' => ""));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "店铺不存在"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="获取店铺所有商品",
     *     path="/index/goods",
     *     tags={"客户端"},
     *     operationId="2",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="店铺id",
     *         in="formData",
     *         name="shop_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function goods()
    {
		$post = $this->input->post();
        if ($post['shop_id'] && $post['session_key']) {
            $list = $this->Store_model->getAllGoodsInfo($post['shop_id'], $post['session_key']);
            if ($list) {
                send_json(array('code' => 2000, 'data' => $list, 'msg' => ""));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "该店铺没有商品"));
            }

        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="绑定第一次注册的商户",
     *     path="/index/customerStore",
     *     tags={"客户端"},
     *     operationId="3",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="店铺id",
     *         in="formData",
     *         name="shop_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Parameter(
     *         description="用户身份秘钥",
     *         in="formData",
     *         name="session_key",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function customerStore()
    {
        $post = $this->input->post();
        if ($post['shop_id'] && $post['session_key']) {
            $result = $this->Customer_model->bindingCustomerStore($post['shop_id'], $post['session_key']);
            if ($result) {
                send_json(array('code' => 2000, 'data' => '', 'msg' => '绑定成功'));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "绑定失败"));
            }

        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Post(
     *     summary="获取商品的详细信息",
     *     path="/index/goodsDetail",
     *     tags={"客户端"},
     *     operationId="9",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="商品id",
     *         in="formData",
     *         name="goods_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function goodsDetail()
    {
        $post = $this->input->post();
        if ($post['goods_id']) {
            $list = $this->Store_model->getGoodsDetail($post['goods_id']);
            if ($list) {
                send_json(array('code' => 2000, 'data' => $list, 'msg' => ""));
            } else {
                send_json(array('code' => 4004, 'data' => '', 'msg' => "商品不存在"));
            }
        } else {
            send_json(array('code' => 4000, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    /**
     * @SWG\Get(
     *     summary="生成店铺二维码",
     *     path="/index/shopCodeCreate",
     *     tags={"商户端"},
     *     operationId="9",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="商户id",
     *         in="query",
     *         name="shop_id",
     *         required=true,
     *         type="integer",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    //签名验证 @1
    private function init()
    {

        $post                = $this->post                = $this->input->post();
        $post['session_key'] = isset($post['session_key']) ? trim($post['session_key']) : '';
        if ($post['session_key'] && $session_key = $post['session_key']) {
            if ($userInfo = $this->db->query("select * from app_user where session_key = '$session_key'")->row_array()) {
                if ($tel = $userInfo['tel']) {
                    if ($shopTel = $this->db->query("select * from app_shop_tel where tel = '$tel'")->row_array()) {
                        $this->shop_id = $shopTel['shop_id'];
                    } else {
                        send_json(array('code' => 4002, 'data' => '', 'msg' => "所登陆账号,没有权限商户权限"));
                    }
                } else {
                    send_json(array('code' => 4002, 'data' => '', 'msg' => "已注销,请重新登陆"));
                }
            } else {
                send_json(array('code' => 4002, 'data' => '', 'msg' => "验签失败，请重新授权登录"));
            }
        } else {
            send_json(array('code' => 4001, 'data' => '', 'msg' => "参数错误"));
        }
    }
    public function shopCodeCreate()
    {
        header('content-type:image/png');

        $get     = $this->input->get();
        $shop_id = $get['shop_id'];
        $data    = array();
        if (isset($get['goods_id']) && $get['goods_id'] != '') {
            $goods_id      = $get['goods_id'];
            $data['scene'] = $shop_id . '#' . $goods_id;
        } else {
            $data['scene'] = $shop_id;
        }

        $data['page'] = "pages/authorization/authorization";
        $data         = json_encode($data);
        $appid        = Conf::getAppId2();
        $secret       = Conf::getAppSecret2();
        $url          = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $access       = json_decode($this->curl_get($url), true);
        $access_token = $access['access_token'];
        $url          = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
        $da           = $this->get_http_array($url, $data);
    }

    public function curl_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err  = curl_error($curl);
        curl_close($curl);
        return $data;
    }

    public function get_http_array($url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出，不用print_r();也会在后面多个1
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        $out = json_decode($output);
        return $out;
    }

    /**
     * @SWG\Post(
     *     summary="定时扫描库存",
     *     path="/index/replenishmentMessage",
     *     tags={"客户端"},
     *     operationId="10",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function replenishmentMessage()
    {
        $list = $this->Store_model->ScanInventory();
    }
}
